package com.example.redisstudy;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.TimeZone;

/**
 * @Classname TimeZoneTest
 * @Description TODO
 * @Version 1.0.0
 * @Date 2024/2/26 14:59
 * @Created by ligang
 */
public class TimeZoneTest {

    public static void main(String[] args) throws ParseException {
        String patterStr = "yyyy-MM-dd";
//        TimeZone.setDefault(TimeZone.getTimeZone("GMT+08"));
        DateFormat dateFormat = new SimpleDateFormat(patterStr);
        String birthdayStr = "1987-06-12 00:00:00.000";
//        String birthdayStr = "1990-06-12";
        // 字符串 -> Date -> 字符串
        Date birthday = dateFormat.parse(birthdayStr);


//        TimeZone.setDefault(TimeZone.getTimeZone(ZoneId.of("GMT+8")));
        long birthdayTimestamp = birthday.getTime();
        Instant instant = birthday.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        // atZone()方法返回在指定时区从此Instant生成的ZonedDateTime。
        LocalDate localDate = instant.atZone(zoneId).toLocalDate();
        System.out.println("老王的生日是：" + birthday);
        System.out.println("老王的生日是localDate：" + localDate);
        System.out.println("老王的生日的时间戳是：" + birthdayTimestamp);

        System.out.println("========================================");
        // 字符串 -> Date -> 时间戳 -> Date -> 字符串
        birthday = new Date(birthdayTimestamp);
        System.out.println("老王的生日是：" + birthday);
        System.out.println("老王的生日的时间戳是：" + dateFormat.format(birthday));
    }
}
